package com.xingongchang.baolebei.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.xingongchang.baolebei.BaolebeiApp;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.activity.ProfileActivity;
import com.xingongchang.baolebei.activity.StartActivity;
import com.xingongchang.baolebei.protocol.TOPIC_COMMENT;


public class TopicCommentAdapter extends BaseAdapter {

	public ArrayList<TOPIC_COMMENT> list = new ArrayList<TOPIC_COMMENT>();
	private LayoutInflater inflater;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	Context context;

	public TopicCommentAdapter(Context context, ArrayList<TOPIC_COMMENT> comments) {
		this.list = comments;
		inflater = LayoutInflater.from(context);
		imageLoader.init(ImageLoaderConfiguration.createDefault(context));
	}

	@Override
	public int getCount() {

		return list.size() == 0 ? 1 : list.size();

	}

	@Override
	public Object getItem(int position) {

		return list.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {

		final ViewHolder holder;
	    View convertView = view;
		if(list.size() != 0){
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.topic_comment_view, null);
				holder.user_photo = (ImageView) convertView
						.findViewById(R.id.user_photo);
				holder.user_name = (TextView) convertView
						.findViewById(R.id.user_name);
				holder.comment_body = (TextView) convertView
						.findViewById(R.id.comment_body);
				holder.datetime = (TextView) convertView
						.findViewById(R.id.datetime);
				holder.floor = (TextView) convertView.findViewById(R.id.floor);
			    TOPIC_COMMENT item = list.get(position);
				imageLoader.displayImage(item.userphoto, holder.user_photo,
						BaolebeiApp.options);
				holder.user_name.setText(item.username);
				if(isHomepage(item.body)==true){
					holder.comment_body.setText(item.body);
				}
				holder.comment_body.setText(item.body);
				holder.datetime.setText(getDateShort(item.creaton));
				holder.floor.setText((position+1)+"楼");
			}else{
				convertView = inflater.inflate(R.layout.comment_empty_view, null);
			}
        
			return convertView;
	}
	class ViewHolder {
		private ImageView user_photo;
		private TextView user_name;
		private TextView comment_body;
		private TextView datetime;
		private TextView floor;

	}
	  private static boolean match( String regex ,String str ){
	        Pattern pattern = Pattern.compile(regex);
	        Matcher  matcher = pattern.matcher( str );
	        return matcher.matches();
	    }
	public static boolean isHomepage( String str ){
        String regex = "http://(([a-zA-z0-9]|-){1,}\\.){1,}[a-zA-z0-9]{1,}-*" ;
        return match( regex ,str );
    }
	/**
	 * 将 2009-7-24 14:28:12 转换为对应的 2009-07-24
	 * 
	 * @param time
	 * @return date
	 */
	public static String getDateShort(String time) {
		String date = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date d = sdf1.parse(time);
			date = sdf.format(d);

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;

	}

}
